package com.michale.admin.controller.monitor;

import com.github.pagehelper.PageInfo;
import com.michale.admin.controller.tool.BaseController;
import com.michale.common.annotation.Log;
import com.michale.common.domain.operate.LoginInfoEntity;
import com.michale.common.enmus.BusinessType;
import com.michale.common.result.AjaxResult;
import com.michale.common.result.PageResult;
import com.michale.framework.security.service.PasswordService;
import com.michale.system.service.LoginInfoService;
import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Michale
 * @apiNote 登录日志管理
 */
@Api(tags = "登录日志管理")
@RestController
@RequestMapping("monitor/loginInfo")
public class LoginInfoController extends BaseController {
    
    @Resource
    private LoginInfoService loginInfoService;
    @Resource
    private PasswordService passwordService;

    /**
     * 查询系统登录日志集合
     *
     * @param loginInfoEntity 分页查询参数
     * @return 系统登录日志集合
     */
    @GetMapping("/loginInfoList")
    @PreAuthorize("@security.isPermission('monitor:loginInfo:list')")
    public PageResult loginInfoList(LoginInfoEntity loginInfoEntity) {
        startPage();
        List<LoginInfoEntity> loginInfoEntities = loginInfoService.selectLoginInfoList(loginInfoEntity);
        PageInfo<LoginInfoEntity> pageInfo = new PageInfo<>(loginInfoEntities);
        return pageResult(pageInfo);
    }

    /**
     * 批量删除系统登录日志
     *
     * @param sysLoginInfoIds 需要删除的登录日志ID
     * @return 结果
     */
    @DeleteMapping("/deleteLoginInfoLog/{sysLoginInfoIds}")
    @Log(title = "删除日志", businessType = BusinessType.CLEAN)
    @PreAuthorize("@security.isPermission('monitor:loginInfo:remove')")
    public AjaxResult deleteLoginInfoLog(@PathVariable Long[] sysLoginInfoIds) {
        return toAjax(loginInfoService.deleteLoginInfoByIds(sysLoginInfoIds));
    }

    /**
     * 根据账户名解锁账户
     *
     * @param userName 用户名
     * @return 结果
     */
    @GetMapping("/unlock/{userName}")
    @PreAuthorize("@security.isPermission('monitor:loginInfo:remove')")
    public AjaxResult unlock(@PathVariable("userName") String userName) {
        passwordService.unlock(userName);
        return success();
    }

    /**
     * 清空登录日志记录
     *
     * @return 结果
     */
    @DeleteMapping("/cleanLoginInfo")
    @Log(title = "清空登录", businessType = BusinessType.DELETE)
    @PreAuthorize("@security.isPermission('monitor:loginInfo:remove')")
    public AjaxResult cleanLoginInfo() {
        loginInfoService.cleanLoginInfo();
        return success();
    }
}
